home *** CD-ROM | disk | FTP | other *** search
- Path: news.iag.net!news
- From: jatmon@iag.net (John R Buchan)
- Newsgroups: comp.lang.c
- Subject: Re: Simple i/o for my class
- Date: 31 Jan 1996 05:08:00 GMT
- Organization: Internet Access Group, Orlando, Florida
- Message-ID: <4emtfg$2i2@news.iag.net>
- References: <4ekjql$5jl@nnrp1.news.primenet.com>
- NNTP-Posting-Host: pm1-orl14.iag.net
- X-Newsreader: WinVN 0.99.7
-
- In article <4ekjql$5jl@nnrp1.news.primenet.com>, mcoplea@primenet.com says...
- >
- >I am having a little trouble with a batting average program.
- >It seems to take the input OK, but always returns a '0' for the Batting
- >Average. Please look at the following code and make any suggestions. Thank
- >you in advance for any assistance you can offer. (email perfered)
- >
- >/***********************************
- > DESIGN:
- > Prompt the user to enter the number of at bats and the number of
- hits.
- > Calculate the batting average (number of hits / number of at bats)
- > Print out the result with 3 significant places to the right of the
- > decimal point and make sure that the average does not round up.
- > Report the results back to the user.
- >
- > TEST PLAN:
- > Large Number / Large Number (11,000 / 2,125)
- > Large Number / Small Number (12,000 / 33)
- > Small Number / Small Number (15 / 6)
- > Large Number / Zero (12,121 / 0)
- > Small Number / Zero (5 / 0)
-
- Divide by zero is illegal. So, you will have to test for and handle it.
-
- >
- >************************************/
- >
- >#include <stdio.h>
- >
- >int
- >main(void)
- >{
- > float avar, hvar; /*float variables(avar=At-bats,hvar=Hits */
- > int Hitsint, Atbatsint; /* Integers with Hits and At Bats */
- > float Avg; /* Results Batting Average */
- >
- > /* Prompt the user to input the data */
- > printf("Enter the number of at-bats followed by hits: ");
- > scanf("%f %f", &avar, &hvar);
-
- scanf is tricky to use for any input, especially multiple numbers on a
- single line (users have a bad habit of not using exactly the format you
- expect >-} This tends to really confuse scanf). You might want to use fgets
- and sscanf (remember to test the return for the number of fields parsed).
-
- >
- > /* Convert to integers */
- > Atbatsint = avar;
- > Hitsint = hvar;
-
- ?? I'm not sure why you scanf these into floats, when you apparently want
- them in ints (and the type of values to be entered are inherently integral.
- At least I don't think you can have a fraction of a hit or at bat?).
-
- >
- > /* Calculate the Batting Average */
- > Avg = Hitsint/Atbatsint;
-
- This is where you are getting your 0. Because Hitsint and AtBatsint are
- both ints, integer math is performed. This means that any fractional
- component is truncated. So, as long as AtBatsint is >= Hitsint, Avg will
- be assigned a value of 0.0.
-
- You can force floating point math by casting either or both of the operands
- to a floating point type (float, double, or long double). Of course, you
- could simple use the floats you originally read the input into (ie use
- avar and hvar instead of Hitsint and AtBatsint).
-
- >
- > /* Return results to user */
- > printf("Batting Average = %.2f\n", Avg);
-
- Didn't the assignment specify precision to 3 places and protection against
- rounding up? This is 2 places and you will have rounding problems.
-
- >
- > return 0; /* We done */
- >}
- >
- >
-
- I strongly suggest that you d/l and read through the c.l.c faq (Frequently
- Asked Question) list. It contains explanations and suggested solutions
- to many common problems in c programming. It is available for anonymous ftp
- from rtfm.mit.edu /pub/usenet/comp.lang.c.
-
- --
- John R Buchan -:|:- Looking for that elusive FAQ? ftp to:
- jatmon@mail.iag.net -:|:- rtfm.mit.edu /pub/usenet-by-group/....
-
-